package org.javaforever.cookieshop.dao;

import java.util.Set;
import org.javaforever.cookieshop.domain.OrderItemBean;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import jakarta.transaction.Transactional;
public interface OrderItemDao extends JpaRepository<OrderItemBean, Long>{
	@Query("from OrderItemBean o")
	public List<OrderItemBean> listAllOrderItems() throws Exception;

	@Modifying
	@Query("delete from OrderItemBean o where o.id = :id")
	@Transactional
	public void deleteOrderItem(Long id) throws Exception;

	@Modifying
	@Query("update OrderItemBean o set o.active = false where o.id = :id")
	@Transactional
	public void softDeleteOrderItem(Long id) throws Exception;

	@Query("from OrderItemBean o where o.id = :id")
	public OrderItemBean findOrderItemById(Long id) throws Exception;

	@Query("from OrderItemBean o where o.name = :name")
	public OrderItemBean findOrderItemByName(String name) throws Exception;

	@Query("from OrderItemBean o where o.active = true")
	public List<OrderItemBean> listActiveOrderItems() throws Exception;

	@Modifying
	@Query("update OrderItemBean o set o.active = not o.active where o.id = :id")
	@Transactional
	public void toggleOrderItem(Long id) throws Exception;

	@Modifying
	@Query("update OrderItemBean o set o.active = true where o.id = :id")
	@Transactional
	public void activateOrderItem(Long id) throws Exception;

	@Query("from OrderItemBean o where o.id = :id")
	public OrderItemBean findOrderItemFullInfoById(Long id) throws Exception;

	@Query("from OrderItemBean o where o.name like CONCAT(:shortName, '%')")
	public List<OrderItemBean> searchOrderItemIndexedNamesByShortName(String shortName) throws Exception;

	@Query("select count(*) as countNum from OrderItemBean o where o.active = true")
	public Integer countActiveOrderItemRecords() throws Exception;

	@Query(value="select distinct cs_order_items.id,cs_order_items.name,cs_order_items.active,cs_order_items.price,cs_order_items.amount,cs_order_items.goods,cs_order_items.cust_order from cs_order_items,cs_cust_order_order_item where cs_order_items.id = cs_cust_order_order_item.order_item_id and cs_cust_order_order_item.cust_order_id = :custOrderId and cs_order_items.active = true order by cs_order_items.name asc ", nativeQuery=true)
	public Set<OrderItemBean> listActiveCustOrderOrderItemsUsingCustOrderId(Long custOrderId) throws Exception;
	
	@Query("from OrderItemBean i where i.custOrder=:orderId")
    public List<OrderItemBean> selectAllItem(Long orderId) throws Exception;
}
